home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / HDRCubeMap / HDRCubeMap.fx < prev    next >
Encoding:
Text File  |  2004-09-28  |  9.3 KB  |  360 lines

  1. //-----------------------------------------------------------------------------
  2. // File: HDRCubeMap.fx
  3. //
  4. // Desc: Effect file for high dynamic range cube mapping sample.
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8.  
  9.  
  10.  
  11.  
  12. #ifndef MAX_NUM_LIGHTS
  13. #define MAX_NUM_LIGHTS 4
  14. #endif
  15.  
  16.  
  17. float4x4 g_mWorldView;
  18. float4x4 g_mProj;
  19. texture  g_txCubeMap;
  20. texture  g_txCubeMap2;
  21. texture  g_txScene;
  22. float4   g_vLightIntensity = { 15.0f, 15.0f, 15.0f, 1.0f };
  23. float4   g_vLightPosView[MAX_NUM_LIGHTS];  // Light positions in view space
  24. float    g_fReflectivity;                  // Reflectivity value
  25.  
  26.  
  27. //-----------------------------------------------------------------------------
  28. // Sampler: samCubeMap
  29. // Desc: Process vertex for HDR environment mapping
  30. //-----------------------------------------------------------------------------
  31. samplerCUBE g_samCubeMap = 
  32. sampler_state
  33. {
  34.     Texture = <g_txCubeMap>;
  35.     MinFilter = Linear;
  36.     MagFilter = Linear;
  37.     MipFilter = Linear;
  38. };
  39.  
  40.  
  41. samplerCUBE g_samCubeMap2 = 
  42. sampler_state
  43. {
  44.     Texture = <g_txCubeMap2>;
  45.     MinFilter = Linear;
  46.     MagFilter = Linear;
  47.     MipFilter = Linear;
  48. };
  49.  
  50.  
  51. sampler2D g_samScene =
  52. sampler_state
  53. {
  54.     Texture = <g_txScene>;
  55.     MinFilter = Linear;
  56.     MagFilter = Linear;
  57.     MipFilter = Linear;
  58. };
  59.  
  60.  
  61. //-----------------------------------------------------------------------------
  62. // Vertex Shader: HDRVertEnvMap
  63. // Desc: Process vertex for HDR environment mapping
  64. //-----------------------------------------------------------------------------
  65. void HDRVertEnvMap( float4 Pos : POSITION,
  66.                     float3 Normal : NORMAL,
  67.                     out float4 oPos : POSITION,
  68.                     out float3 EnvTex : TEXCOORD0 )
  69. {
  70.     oPos = mul( Pos, g_mWorldView );
  71.  
  72.     //
  73.     // Compute normal in camera space
  74.     //
  75.     float3 vN = mul( Normal, g_mWorldView );
  76.     vN = normalize( vN );
  77.  
  78.     //
  79.     // Obtain the reverse eye vector
  80.     //
  81.     float3 vEyeR = -normalize( oPos );
  82.  
  83.     //
  84.     // Compute the reflection vector
  85.     //
  86.     float3 vRef = 2 * dot( vEyeR, vN ) * vN - vEyeR;
  87.  
  88.     //
  89.     // Store the reflection vector in texcoord0
  90.     //
  91.     EnvTex = vRef;
  92.  
  93.     //
  94.     // Apply the projection
  95.     //
  96.     oPos = mul( oPos, g_mProj );
  97. }
  98.  
  99.  
  100. //-----------------------------------------------------------------------------
  101. // Pixel Shader: HDRPixEnvMap
  102. // Desc: Process pixel for HDR environment mapped object
  103. //-----------------------------------------------------------------------------
  104. float4 HDRPixEnvMap( float3 Tex : TEXCOORD0 ) : COLOR
  105. {
  106.     return g_fReflectivity * texCUBE( g_samCubeMap, Tex );
  107. }
  108.  
  109.  
  110. float4 HDRPixEnvMap2Tex( float3 Tex : TEXCOORD0 ) : COLOR
  111. {
  112.     return g_fReflectivity * float4( texCUBE( g_samCubeMap, Tex ).xy, texCUBE( g_samCubeMap2, Tex ).xy );
  113. }
  114.  
  115.  
  116. //-----------------------------------------------------------------------------
  117. // Vertex Shader: HDRVertScene
  118. // Desc: Process vertex for HDR-enabled scene
  119. //-----------------------------------------------------------------------------
  120. void HDRVertScene( float4 iPos : POSITION,
  121.                    float3 iNormal : NORMAL,
  122.                    float2 iTex : TEXCOORD0,
  123.                    out float4 oPos : POSITION,
  124.                    out float2 Tex : TEXCOORD0,
  125.                    out float3 Pos : TEXCOORD1,
  126.                    out float3 Normal : TEXCOORD2 )
  127. {
  128.     //
  129.     // Transform position to view space
  130.     //
  131.     oPos = mul( iPos, g_mWorldView );
  132.  
  133.     //
  134.     // Also write view position to texcoord1 to do per-pixel lighting
  135.     //
  136.     Pos = oPos;
  137.  
  138.     //
  139.     // Transform to screen coord
  140.     //
  141.     oPos = mul( oPos, g_mProj );
  142.  
  143.     //
  144.     // Transform normal and write to texcoord2 for per-pixel lighting
  145.     //
  146.     Normal = normalize( mul( iNormal, (float3x3)g_mWorldView ) );
  147.     
  148.     //
  149.     // Propagate texture coord
  150.     //
  151.     Tex = iTex;
  152. }
  153.  
  154.  
  155. //-----------------------------------------------------------------------------
  156. // Pixel Shader: HDRPixScene
  157. // Desc: Process pixel (do per-pixel lighting) for HDR-enabled scene
  158. //-----------------------------------------------------------------------------
  159. float4 HDRPixScene( float2 Tex : TEXCOORD0,
  160.                     float3 Pos : TEXCOORD1,
  161.                     float3 Normal : TEXCOORD2 ) : COLOR
  162. {
  163.     float3 N = normalize( Normal );
  164.  
  165.     // Variable to save lit value by each light
  166.     float4 vPixValue = (float4)0;
  167.  
  168.     //
  169.     // Iterate through each light and apply the light on the pixel
  170.     //
  171.     for( int LightIndex = 0; LightIndex < MAX_NUM_LIGHTS; ++LightIndex )
  172.     {
  173.         //
  174.         // Compute light vector (pixel to light)
  175.         //
  176.         float3 vRLightVec = (float3)(g_vLightPosView[LightIndex] - Pos);
  177.  
  178.         //
  179.         // Find out the light intensity at the vertex based on
  180.         // N dot L and distance from the light.
  181.         //
  182.         float fDiffuse = saturate( dot( normalize( vRLightVec ), N ) );
  183.  
  184.         //
  185.         // Attenuation is 1 / D^2. Clamp at 1 to avoid infinity.
  186.         //
  187.         float fAttenuation = saturate( 1.0f / dot( vRLightVec, vRLightVec ) );
  188.  
  189.         //
  190.         // Compute and add pixel color to final value
  191.         //
  192.         vPixValue += fDiffuse * fAttenuation;
  193.     }
  194.  
  195.     return tex2D( g_samScene, Tex ) * vPixValue * g_vLightIntensity;
  196. }
  197.  
  198.  
  199. float4 HDRPixScene_FirstHalf( float2 Tex : TEXCOORD0,
  200.                               float3 Pos : TEXCOORD1,
  201.                               float3 Normal : TEXCOORD2 ) : COLOR
  202. {
  203.     return HDRPixScene( Tex, Pos, Normal ).xyzw;
  204. }
  205.  
  206.  
  207. float4 HDRPixScene_SecondHalf( float2 Tex : TEXCOORD0,
  208.                                float3 Pos : TEXCOORD1,
  209.                                float3 Normal : TEXCOORD2 ) : COLOR
  210. {
  211.     return HDRPixScene( Tex, Pos, Normal ).zwww;
  212. }
  213.  
  214.  
  215. //-----------------------------------------------------------------------------
  216. // Vertex Shader: HDRVertLight
  217. // Desc: Process vertex for light objects
  218. //-----------------------------------------------------------------------------
  219. void HDRVertLight( float4 iPos : POSITION,
  220.                    out float4 oPos : POSITION,
  221.                    out float4 Diffuse : TEXCOORD1 )
  222. {
  223.     //
  224.     // Transform position to screen space
  225.     //
  226.     oPos = mul( iPos, g_mWorldView );
  227.     oPos = mul( oPos, g_mProj );
  228.  
  229.     //
  230.     // Diffuse color is the light intensity value
  231.     //
  232.     Diffuse = g_vLightIntensity;
  233. }
  234.  
  235.  
  236. //-----------------------------------------------------------------------------
  237. // Pixel Shader: HDRPixLight
  238. // Desc: Process pixel for HDR-enabled scene
  239. //-----------------------------------------------------------------------------
  240. float4 HDRPixLight( float4 Diffuse : TEXCOORD1 ) : COLOR
  241. {
  242.     //
  243.     // Diffuse has the full intensity of the light.
  244.     // Just output it.
  245.     //
  246.     return Diffuse;
  247. }
  248.  
  249.  
  250. float4 HDRPixLight_FirstHalf( float4 Diffuse : TEXCOORD1 ) : COLOR
  251. {
  252.     //
  253.     // Diffuse has the full intensity of the light.
  254.     // Just output it.
  255.     //
  256.     return Diffuse.xyww;
  257. }
  258.  
  259.  
  260. float4 HDRPixLight_SecondHalf( float4 Diffuse : TEXCOORD1 ) : COLOR
  261. {
  262.     //
  263.     // Diffuse has the full intensity of the light.
  264.     // Just output it.
  265.     //
  266.     return Diffuse.zwww;
  267. }
  268.  
  269.  
  270. //-----------------------------------------------------------------------------
  271. // Technique: RenderScene
  272. // Desc: Renders scene objects
  273. //-----------------------------------------------------------------------------
  274. technique RenderScene
  275. {
  276.     pass p0
  277.     {
  278.         VertexShader = compile vs_1_1 HDRVertScene();
  279.         PixelShader = compile ps_2_0 HDRPixScene();
  280.     }
  281. }
  282.  
  283.  
  284. technique RenderSceneFirstHalf
  285. {
  286.     pass p0
  287.     {
  288.         VertexShader = compile vs_1_1 HDRVertScene();
  289.         PixelShader = compile ps_2_0 HDRPixScene_FirstHalf();
  290.     }
  291. }
  292.  
  293.  
  294. technique RenderSceneSecondHalf
  295. {
  296.     pass p0
  297.     {
  298.         VertexShader = compile vs_1_1 HDRVertScene();
  299.         PixelShader = compile ps_2_0 HDRPixScene_SecondHalf();
  300.     }
  301. }
  302.  
  303.  
  304. //-----------------------------------------------------------------------------
  305. // Technique: RenderLight
  306. // Desc: Renders light objects
  307. //-----------------------------------------------------------------------------
  308. technique RenderLight
  309. {
  310.     pass p0
  311.     {
  312.         VertexShader = compile vs_1_1 HDRVertLight();
  313.         PixelShader = compile ps_2_0 HDRPixLight();
  314.     }
  315. }
  316.  
  317.  
  318. technique RenderLightFirstHalf
  319. {
  320.     pass p0
  321.     {
  322.         VertexShader = compile vs_1_1 HDRVertLight();
  323.         PixelShader = compile ps_2_0 HDRPixLight_FirstHalf();
  324.     }
  325. }
  326.  
  327.  
  328. technique RenderLightSecondHalf
  329. {
  330.     pass p0
  331.     {
  332.         VertexShader = compile vs_1_1 HDRVertLight();
  333.         PixelShader = compile ps_2_0 HDRPixLight_SecondHalf();
  334.     }
  335. }
  336.  
  337.  
  338. //-----------------------------------------------------------------------------
  339. // Technique: RenderEnvMesh
  340. // Desc: Renders the HDR environment-mapped mesh
  341. //-----------------------------------------------------------------------------
  342. technique RenderHDREnvMap
  343. {
  344.     pass p0
  345.     {
  346.         VertexShader = compile vs_1_1 HDRVertEnvMap();
  347.         PixelShader = compile ps_2_0 HDRPixEnvMap();
  348.     }
  349. }
  350.  
  351.  
  352. technique RenderHDREnvMap2Tex
  353. {
  354.     pass p0
  355.     {
  356.         VertexShader = compile vs_1_1 HDRVertEnvMap();
  357.         PixelShader = compile ps_2_0 HDRPixEnvMap2Tex();
  358.     }
  359. }
  360.